package goods

import (
	"gorm.io/gorm"
	"shopstar/models"
	"shopstar/models/goods"
)

type Goods struct{}

var GoodsService = new(Goods)

func (g *Goods) GoodsAttributeList(id int64) ([]goods.Attribute, error) {
	var goods goods.Goods
	err := models.DB.Where("id = ?", id).Preload("Attribute", func(db *gorm.DB) *gorm.DB {
		return db.Preload("Detail", func(db *gorm.DB) *gorm.DB {
			return db.Where("goods_id = ?", id).Order("sort")
		}).Order("sort")
	}).First(&goods).Error
	//fmt.Println(goods)
	return goods.Attribute, err
}

// GoodsAttributeCount 统计sku的所有数据数
func (g *Goods) GoodsAttributeCount(id int64) int {
	var c int64
	models.DB.Model(goods.AttributeDetail{}).Where("goods_id = ?", id).Count(&c)
	return int(c)
}
